package com.ldc.wandroid.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.ldc.common.mvvm.ViewModelBase;
import com.ldc.common.net.Api2Request;
import com.ldc.wandroid.model.ArticleModel;
import com.ldc.wandroid.model.BannerModel;
import com.ldc.wandroid.server.ApiServer;

import java.util.ArrayList;
import java.util.List;

public class HomeViewModel extends ViewModelBase {
    private final ApiServer apiServer = Api2Request.getInstance().createServer(ApiServer.class);
    private final MutableLiveData<List<BannerModel>> bannerList = new MutableLiveData<>(new ArrayList<>());
    private final MutableLiveData<ArticleModel> articleModel = new MutableLiveData<>();

    public LiveData<List<BannerModel>> getBanner() {
        return bannerList;
    }


    public LiveData<ArticleModel> getArticle() {
        return articleModel;
    }


    public void loadBanner() {
        apiRequest(apiServer.getBanner(), (success, result, throwable) -> {
            if (null == result) {
                return;
            }
            if (result.isSuccess()) {
                bannerList.setValue(result.getData());
            }
        });
    }

    public void loadArticle(int page) {
        apiRequest(apiServer.get_home_article(page), (success, result, throwable) -> {
            if (null == result) {
                return;
            }
            if (result.isSuccess()) {
                articleModel.setValue(result.getData());
            }
        });
    }
}
